<?php
/***
 * @name : 牌号厂家api
 * @Class : BrandAction
 * @Author : yu
 * @Create : 2017-2-12
 * @copyright : Fiidee.Monda.Cn
 */
namespace brand\action;

use herosphp\core\Controller;
use herosphp\core\Log;
use herosphp\http\HttpRequest;
use herosphp\utils\AjaxResult;
use herosphp\bean\Beans;
use api\utils\SaasApiClient;

/**--tmpl-use--**/
class BrandAction extends Controller
{


    /**
     * 类型
     * @return AjaxResult
     */
    public function type(HttpRequest $request)
    {

        $url = "api/brand/getTypes";
        $result = SaasApiClient::send($url, '');

        if ($result->isSucess()) {

            echo json_encode($result->getData());

        } else {
            //20170208 By Wong 当为空或者数据异常返回空数据
            Log::info($result->getMessage());
            echo json_encode(array());
        }
        die;
    }

    /**
     * 品名
     * @param $type_id 类型id
     * @return AjaxResult
     */
    public function name(HttpRequest $request)
    {
        $type_id = $request->getParameter('type_id');
        $name = $request->getParameter('name');

        $url = "api/brand/getProducts";
        $result = SaasApiClient::send($url, array('typeId' => $type_id, 'name' => $name));

        $letterArr = array('A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z');

        if ($result->isSucess()) {

            $resultData=array();

            $items = $result->getData();

            foreach ($letterArr As $k1 => $letter) {

                $lt_items=array();

                foreach ($items As $k2 => $item) {

                    if(matchFirstLetter($item['name'], $letter)){

                        $lt_items[]=$item;
                    }
                }
                if($lt_items){

                    $resultData[]=array('lt'=>$letter,'lt_items'=>$lt_items);

                }

            }

            echo json_encode($resultData);
           // echo json_encode($result->getData());

        } else {
            //20170208 By Wong 当为空或者数据异常返回空数据
            Log::info($result->getMessage());
            echo json_encode(array());
        }
        die;
    }



    /**
     * 厂家
     * @param $type_id  类型id
     * @param $product_id 产品id
     * @return AjaxResult
     */
    public function factory(HttpRequest $request)
    {
        $type_id = $request->getParameter('type_id');
        $product_id = $request->getParameter('product_id');
        $name = $request->getParameter('name');

        $url = "api/brand/getFactorys";
        $result = SaasApiClient::send($url, array('typeId' => $type_id, 'productId' => $product_id, 'name' => $name));
        if ($result->isSucess()) {

            echo json_encode($result->getData());

        } else {
            //20170208 By Wong 当为空或者数据异常返回空数据
            Log::info($result->getMessage());
            echo json_encode(array());
        }
        die;

    }

    /**
     * 牌号
     * @param $type_id 类型id
     * @param $product_id 产品id
     * @param $factory_id 厂家id
     * @return AjaxResult
     */
    public function brand(HttpRequest $request)
    {
        $type_id = $request->getParameter('type_id');
        $product_id = $request->getParameter('product_id');
        $factory_id = $request->getParameter('factory_id');

        $url = "api/brand/getBrands";
        $result = SaasApiClient::send($url, array('typeId' => $type_id, 'productId' => $product_id, 'factoryId' => $factory_id));
        if ($result->isSucess()) {

            echo json_encode($result->getData());

        } else {
            //20170208 By Wong 当为空或者数据异常返回空数据
            Log::info($result->getMessage());
            echo json_encode(array());
        }
        die;

    }



    public function getCache($key)
    {
        $cache = CacheFactory::create();
        $data = $cache->get($key);

        if (!$data) {
            $cache->set($key, $data, 60 * 5);
        }
        return $data;
    }


    public function setCache($data, $key)
    {
        $cache = CacheFactory::create();
        $cache->set($key, $data, 60 * 5);
    }


}
